package com.dick.base.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dick.base.aop.SmartQuery;
import com.dick.base.mapper.BusinessCustomerMapper;
import com.dick.base.model.BusinessCustomer;
import com.dick.base.parameter.CustomerParameter;
import com.dick.base.util.DateUtil;
import com.dick.base.util.IdUtil;
import com.dick.base.util.PageInfo;
import com.dick.base.util.SmartQueryUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 客户service
 */
@Service
public class CustomerService {

    @Autowired
    private BusinessCustomerMapper businessCustomerMapper;

    /**
     * 新增或修改客户
     * @param parameter
     */
    @Transactional
    public void saveCustomer(CustomerParameter parameter) {
        BusinessCustomer customer = new BusinessCustomer();
        customer.setAddress(parameter.getAddress());
        customer.setName(parameter.getName());
        customer.setNickname(parameter.getNickname());
        customer.setId(parameter.getId());
        if (customer.getId() == null) {
            customer.setCreateTime(DateUtil.now());
            customer.setId(IdUtil.nextId());
            businessCustomerMapper.insert(customer);
        } else {
            customer.setUpdateTime(DateUtil.now());
            businessCustomerMapper.updateById(customer);
        }
    }

    /**
     * 客户分页查询
     * @param customer
     * @param pageInfo
     * @return
     */
    @SmartQuery(clazz = BusinessCustomer.class, likeFields = {"name", "nickname", "address"})
    public Page<BusinessCustomer> customerPage(BusinessCustomer customer, PageInfo pageInfo) {
        return businessCustomerMapper.selectPage(SmartQueryUtil.getPage(), SmartQueryUtil.getQueryWrapper());
    }

    /**
     * 客户列表
     * @return
     */
    public List<BusinessCustomer> customerList() {
        return businessCustomerMapper.selectList(null);
    }
}
